package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.query.StrategyCatalogQuery;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.cache.service.IDestinationStatisService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/destination")
public class DestinationController {

    @Reference
    private IDestinationService destinationService;

    @Reference
    private IRegionService regionService;

    @Reference
    private IStrategyCatalogService strategyCatalogService;

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private ITravelService travelService;

    @Reference
    private IDestinationStatisService destinationStatisService;

    @RequestMapping("")
    public String index(Model model){
        List<Region> regionList = regionService.queryHotRegions();
        model.addAttribute("hotRegions",regionList);
        return "destination/index";
    }
    @RequestMapping("/getHotDestByRegionId")
    public Object getHotDestByRegionId(@ModelAttribute("regionId") Long regionId, Model model){
        List<Destination> destinations = destinationService.getDestByRegionId(regionId);
        int size = destinations.size();
        List<Destination> leftDests = destinations.subList(0,size/2+1);
        List<Destination> rightDests = destinations.subList(size/2+1,size);
        model.addAttribute("leftDests",leftDests);
        model.addAttribute("rightDests",rightDests);
        return "destination/hotdestTpl";
    }

    /**
     *
     * @param model
     * @param id 目的地id
     * @return
     */
    @RequestMapping("/guide")
    public String guide(Model model,Long id){
        List<Destination> toasts = destinationService.getToasts(id);
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts",toasts);
        model.addAttribute("dest",dest);
        List<StrategyCatalog> catalog = strategyCatalogService.getCatalogByDestId(id);
        model.addAttribute("catalogs",catalog);

        //点击量前三的攻略
        List<StrategyDetail> strategyDetails = strategyDetailService.queryStrategyTop3ByDestId(id);
        model.addAttribute("strategyDetails",strategyDetails);
        model.addAttribute("type","概览");
        return "destination/guide";
    }

    @RequestMapping("/travels")
    public String travels(Model model, @ModelAttribute("qo")TravelQuery qo){
        PageInfo pageInfo = travelService.query(qo);
        model.addAttribute("pageInfo",pageInfo);

        return "destination/travelTpl";

    }

    @RequestMapping("surveyPage")
    public String surveyPage(Model model, @ModelAttribute("qo")StrategyCatalogQuery qo){

        //qo  toast dept
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts",toasts);
        model.addAttribute("dest",dest);
        return "destination/survey";
    }
    @RequestMapping("survey")
    public String survey(Model model, @ModelAttribute("qo")StrategyCatalogQuery qo){

        //qo  toast dept
        List<StrategyCatalog> catalogs = strategyCatalogService.getCatalogByDestId(qo.getDestId());
        model.addAttribute("catalogs",catalogs);
        StrategyCatalog strategyCatalog = new StrategyCatalog();
        if(catalogs != null && catalogs.size() > 0){
            for (StrategyCatalog catalog : catalogs) {
                if(catalog.getId() == qo.getCatalogId()){
                    model.addAttribute("catalog",catalog);
                    strategyCatalog = catalog;
                    break;
                }
            }
        }
        if(strategyCatalog != null){
            List<StrategyDetail> details = strategyCatalog.getDetails();
            if(details != null && details.size() > 0){
                StrategyDetail strategyDetail1 = details.get(0);
                StrategyContent content = strategyDetailService.getContent(strategyDetail1.getId());
                strategyDetail1.setStrategyContent(content);
                model.addAttribute("detail",strategyDetail1);
            }
        }

        return "destination/surveyTpl";

    }

    /**
     * 用户收藏目的地
     * @param destId
     * @param userInfo
     * @return
     */
    @ResponseBody
    @RequestMapping("/favor")
    public Object favor(Long destId, @UserParam UserInfo userInfo){
        AjaxResult result = new AjaxResult();
        if (userInfo==null){
            result.setSuccess(false);
            result.setCode(102);
            return result;
        }
        boolean favor = destinationStatisService.favor(destId, userInfo.getId());
        if (favor){
            result.setSuccess(true);
        }else {
            result.setSuccess(false);
        }
        return result;
    }


}
